<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'file.uploadSetting.add'"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del" v-auth="'file.uploadSetting.deleteBatch'"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.title" placeholder="设置名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="设置名称" prop="title" width="150"></el-table-column>
				<el-table-column label="上传码" prop="code" width="150"></el-table-column>
				<el-table-column label="上传目录" prop="fullDir" width="150"></el-table-column>
				<el-table-column label="上传大小限制(MB)" prop="maxSize" width="150"></el-table-column>
				<el-table-column label="存储方式" prop="location" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="160">
					<template #default="scope">
						<el-button-group>
							<el-button type="primary" text size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button type="primary" text size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'file.uploadSetting.edit'">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)" >
								<template #reference>
									<el-button type="primary" text size="small" v-auth="'file.uploadSetting.deleteBatch'">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>


</template>

<script>
	import saveDialog from './save'

	export default {
		name: 'uploadSetting',
		components: {
			saveDialog
		},
		data() {
			return {
				dialog: {
					save: false
				},
				selection: [],
				apiObj: this.$API.file.uploadSetting.list,
				search: {
					title: null
				}
			}
		},
		methods: {
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			async table_del(row){
				var res = await this.$API.file.uploadSetting.delete.delete(row.id);
				if(res.success){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}
			},
			//批量删除
			async batch_del(){
				this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning'
				}).then(async() => {
					const loading = this.$loading();
					let ids = ""
					this.selection.forEach(function(e) {
						ids += e.id + ","
					})
					ids = ids.substring(0, ids.length - 1)
					var res = await this.$API.file.uploadSetting.delete.delete(ids);
					if(res.success){
						this.$refs.table.refresh()
						this.$message.success("操作成功")
					}
					loading.close();
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh()
				}else if(mode=='edit'){
					this.$refs.table.refresh()
				}
			}
		},
		mounted() {
		}
	}
</script>

<style>
</style>
